<?php defined( 'BASEPATH' ) or die( 'Restricted' );

/*
This code is copyright 2009-2011 by TMLA INC.  ALL RIGHTS RESERVED.
Please view license.txt in /tgsf_core/legal/license.txt or
http://tgWebSolutions.com/opensource/tgsf/license.txt
for complete licensing information.
*/

$form =& load_form( 'new_link' );
$form->processor( URL( 'new' ) );

$links_model = load_model( 'links' );

//------------------------------------------------------------------------

if ( POST()->dataPresent )
{
	$form->ds( clone POST() );
	$form->validate();

	if ( $form->valid )
	{
		if ( starts_with( $form->ds->link_url, 'http://' ) == false )
		{
			if ( starts_with( $form->ds->link_url, 'https://' ) == false )
			{
				$form->ds->setVar( 'link_url', 'http://' . $form->ds->link_url );
			}
		}

		load_library( 'dereference' );
		load_library( 'htmlpurifier/library/HTMLPurifier.auto' );
		$hpc = HTMLPurifier_Config::createDefault();
		$hpc->set('HTML.Doctype', 'XHTML 1.0 Transitional');
		$purifier = new HTMLPurifier( $hpc );
		
		$url = dereferenceUrl( $form->ds->link_url );

		if ( $url === false )
		{
			URL( '' )->setVar( 'm', 'redirects' )->redirect();
		}
		
		if ( $url instanceOf tgsfDataSource == false && $url === '404' )
		{
			URL( '' )->setVar( 'm', '404' )->redirect( URLredirectExit, URLjsonOnAjax );
		}
		
		if ( $url == 'too-many-retries' )
		{
			URL( '' )->setVar( 'm', 'retries' )->redirect( URLredirectExit, URLjsonOnAjax );
		}

		$title = trim( (string)$purifier->purify( $url->title ) );

		$form->ds->setVar( 'link_url', $url->url );
		$form->ds->setVar( 'link_page_title', $title );

		try
		{
			dbm()->beginTransaction();
			$updated = $links_model->urlExistsAndUpdate( $form->ds->link_url );
			
			if ( $updated == linkExistsUpdated || $updated == linkExistsTooNew )
			{
				// handle tags
				$links_model->addTagsToLink( $form->ds->link_url, $form->ds->link_tags );
			}

			if ( $updated == linkExistsUpdated )
			{
				$url = URL( '' )->setVar( 'm', 'exists-updated' )->redirect( URLredirectExit, URLjsonOnAjax );
			}
		
			if ( $updated == linkExistsTooNew )
			{
				URL( '' )->setVar( 'm', 'exists-recent' )->redirect( URLredirectExit, URLjsonOnAjax );
			}

			$ds = $form->ds;

			$ds->setVar( 'link_description', (string)$purifier->purify( $ds->link_description ) );

			$dirty = trim( '<a href="' ) . trim( $ds->link_url ) . '">' . '---' . trim( '</a>' );
			$clean = trim( (string)$purifier->purify( $dirty ) );
			
			$pieces = explode( '<a href="', $clean );
			
			if ( count( $pieces ) == 2 && starts_with( $pieces[1], 'http' ) )
			{
				list( $url ) = explode( '">---</a>', $pieces[1] );
			}
			else
			{
				dbm()->rollback();
				URL( '' )->setVar( 'm', 'content' )->redirect( URLredirectExit, URLjsonOnAjax );
			}

			$links_model->insert( $form->ds );
			$links_model->addTagsToLink( $form->ds->link_url, $form->ds->link_tags );
			dbm()->commit();
			URL( '' )->setVar( 'm', 'success' )->redirect( URLredirectExit, URLjsonOnAjax );
		}
		catch ( Exception $e )
		{
			show_error( 'An error occurred.', $e );
		}
		URL( '' )->redirect();
	}
	else // invalid form post
	{
		$form->ajaxErrors();
	}
}


$formHtml  = $form->render();
$formValid = $form->valid;

//------------------------------------------------------------------------

$metaDescription = 'Add a new Link';
$windowTitle = 'Add a new link';
$pageTitle = 'Add a New Link';

include view( 'header' );
include view( 'template/top' );
include view( 'new' );
include view( 'footer' );
